/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package businessLogic;

import java.sql.*;

/**
 *
 * @author cloud
 */

public class DBManager 
{
    public String accessDB()
    {
        String url = "jdbc:derby://localhost:1527/sample";
        String user = "app";
        String pwd = "app";
        
        try
        {
            DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver());
            // crea la connessione con il DB
            Connection c = DriverManager.getConnection(url, user, pwd);
            //codice per interrogazione delle tabelle del DB
            Statement st = c.createStatement();
            
            //in rs avrò il risultato della query
            ResultSet rs = st.executeQuery("SELECT * FROM CUSTOMER");
            
            String result = ""; 
            while (rs.next())
            {
               System.out.println("NAME = "+ rs.getString("NAME")+ ", CITY = "+rs.getString("CITY"));
               result +=  "NAME = "+ rs.getString("NAME")+ ", CITY = "+rs.getString("CITY") +"\n\r";
                
            }
            
            //importante chiudere le connessioni perchè sono in numero limitato
            rs.close();
            st.close();
            
            c.close();
            
            
            return result;
        }
        catch(SQLException ex)
        {
            System.out.println(ex.getMessage());
        }
        return null;
    }
}
